import { Column, Entity, JoinTable, ManyToMany } from 'typeorm'
import { CommonEntity } from '../common.entity'
import { UserEntity } from './user.entity'
import { MenuEntity } from './menu.entity'

@Entity('sys_role')
export class RoleEntity extends CommonEntity {
  @Column({ comment: '角色编码', unique: true })
  code: string

  @Column({ comment: '角色名称', default: null })
  name: string

  @Column({ comment: '角色权级', default: 0 })
  level: number

  /** 数据状态（1正常，0停用） */
  @Column({ type: 'tinyint', comment: '数据状态', default: 1 })
  status: number

  @Column({ comment: '角色备注', default: null })
  remark: string

  @ManyToMany(() => UserEntity, (user) => user.roles, { onDelete: 'CASCADE' })
  users: UserEntity[]

  @ManyToMany(() => MenuEntity, (menu) => menu.roles)
  @JoinTable({ name: 'sys_role_menu_relation', joinColumns: [{ name: 'roleId' }], inverseJoinColumns: [{ name: 'menuId' }] })
  menus: MenuEntity[]
}
